<extend name="./Application/Admin/View/Layout/application.html"/>

<block name="content">
    <div class="admin-content">
        <div class="am-cf am-padding">
            <div class="am-fl am-cf"><strong class="am-text-primary am-text-lg">用户管理</strong> /
                <small>Users Manage</small>
            </div>
        </div>

        <div class="am-g">
            <div class="am-u-sm-12 am-u-md-6">
                <div class="am-btn-toolbar">
                    <div class="am-btn-group am-btn-group-xs">
                        <a class="am-btn am-btn-default" href="{{:U('AuthSet/add_user')}}">
                            <span class="am-icon-plus"></span> 新增
                        </a>
                    </div>
                </div>
            </div>

            <div class="am-u-sm-12 am-u-md-3">
                <form>
                    <div class="am-input-group am-input-group-sm">
                        <input type="text" class="am-form-field" name="username" value="">
                        <span class="am-input-group-btn">
                            <button class="am-btn am-btn-default" type="submit">搜索</button>
                        </span>
                    </div>
                </form>
            </div>
        </div>

        <div class="am-g">
            <div class="am-u-sm-12">
                <table class="am-table am-table-striped am-table-hover table-main">
                    <thead>
                    <tr>
                        <th class="table-id">编号</th>
                        <th class="table-thumb">用户名</th>
                        <th class="table-thumb">所属用户组</th>
                        <th class="table-name">邮箱</th>
                        <th>创建时间</th>
                        <th>账号状态</th>
                        <th class="table-set">操作</th>
                    </tr>
                    </thead>
                    <tbody>

                    <volist name="user_manages" id="item">
                        <tr data-uid="{{$item.id}}">
                            <td>{{$item.id}}</td>
                            <td>{{$item.username}}</td>
                            <td>{{$item.groups|pluck=###, 'title'|implode=', '}}</td>
                            <td>{{$item.email}}</td>
                            <td>{{$item.created_at|date="Y-m-d H:i:s", ###}}</td>
                            <td data-status="{{$item.status}}">
                                <eq name="item.status" value="1">
                                    <a href="javascript:;" class="am-btn am-btn-success am-btn-xs status">已启用</a>
                                    <else/>
                                    <a href="javascript:;" class="am-btn am-btn-danger am-btn-xs status">已禁用</a>
                                </eq>
                            </td>
                            <td>
                                <div class="am-btn-toolbar">
                                    <div class="am-btn-group am-btn-group-xs">
                                        <a class="am-btn am-btn-default am-btn-xs am-text-secondary"
                                           href="{{:U('edit_user', array('id'=>$item[id]))}}">
                                            <span class="am-icon-pencil-square-o"></span> 编辑
                                        </a>

                                        <neq name="item.id" value="1">
                                            <a class="am-btn am-btn-default am-btn-xs am-text-danger am-hide-sm-only delete"
                                               href="javascript:;">
                                                <span class="am-icon-trash-o"></span> 删除
                                            </a>
                                        </neq>
                                    </div>
                                </div>
                            </td>
                        </tr>
                    </volist>
                    </tbody>
                </table>

                共 {{$usersNum}} 条记录
                <hr/>
            </div>
        </div>
    </div>
</block>

<block name="js">
    <script>
        $(function () {
            //ajax删除用户
            $('.delete').click(function () {
                var uid = $(this).parents('tr').data('uid');
                $.post("{{:U('delete_user')}}", {
                    uid: uid
                }, function (data) {
                    if (data.status == 1) {
                        alert(data.msg);
                        $("tr[data-uid='" + uid + "']").fadeOut(400);
                    } else {
                        alert(data.info); //如果无法新增就提示没有权限信息，然后跳转
                        location.href = "{{:U('User/no_auth')}}";
                    }
                })
            });

            //启用禁用
            $('.status').click(function () {
                var id = $(this).parents('tr').data('uid');
                var status = $(this).parents('td').attr('data-status');
//                var _this = $(this);
                $.post("{{:U('change_status')}}", {
                    id: id,
                    status: status
                }, function () {
                    window.location.reload();
//                    _this.toggleClass('am-btn-success am-btn-danger');
                })
                return false;
            })
        })
    </script>
</block>